var app = angular.module("app",[]);



app.factory('User',function(){
  return {nick: ""};

});

app.config(function($routeProvider){
	$routeProvider
	.when('/',
		{ templateUrl:"login.html",controller: "loginController"})
  .when('/rooms',
    { templateUrl:"rooms.html",controller:"roomsController"})
  .when('/room/:id/',
    { templateUrl:"room.html",controller:"roomController"})
	.otherwise(
		{template: "End of the multiverse!!!!"})
	;
});

app.controller("indexController", ["$http","$scope","$location","User",function ($http,$scope,$location,User) {

    $scope.logOut = function(){
      User.nick = "";
      console.log(User.nick);
      $location.path("/");
    }
  
}]);

app.controller("loginController",["$http","$scope","$location","User",function ($http,$scope,$location,User) {
  console.log("nick is: " +User.nick);
  if(User.nick == ""){
     $scope.login = function(){
      console.log("nick is: " + $scope.form.msg);
          User.nick = $scope.form.msg;
          console.log("here : " + User.nick);
          $http({method: 'POST', url: '/login',data:User}).
        success(function(data, status, headers, config) {
          $location.path("/rooms");
        }).
        error(function(data, status, headers, config) {
        	console.log("loginCtrl Error");
        });
        
        //$location.path("/hallo"+"/:"+x.nick);
        }
      }
      else{
        $location.path("/rooms");
      }
}]);
 app.controller("roomsController",["$http","$scope","$location","User",function ($http,$scope,$location,User) {
         $http({method: 'GET', url: '/rooms'}).
          success(function(data, status, headers, config) {
            $scope.channels = data.rooms;
          }).
          error(function(data, status, headers, config) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
          });
         $scope.joinChannel = function(id){
            console.log(User.nick);
            $http({method: 'POST', url: '/room/'+id, data:User}).
             success(function(data,status,headers,config){
              if(status == 200){
                
              }
             }).
             error(function(data, status, headers, config) {
              console.log("roomctrl error");
            
             });
             $location.path("/room/"+id);
           }
         $scope.createChannel = function(){
            x = $scope.form;

            $http({method: 'POST', url: '/room/create',data:x}).
             success(function(data, status, headers, config) {
            if(status == 200)
            {
              $scope.channels = data.rooms;
              //$location.path("rooms");
            }

          }).
          error(function(data, status, headers, config) {
            console.log("roomsctrl error");
            // called asynchronously if an error occurs
            // or server returns response with an error status.
          });
        }

        // the last received msg);
        $scope.msg = {};
  }]);
app.controller("roomController",["$http","$scope","$location","User",function ($http,$scope,$location,User) {


    /*var source = new EventSource($location.path()+'update');
        source.addEventListener('message', handleCallback, false);*/

     $scope.sendMessage = function(id){
        console.log($scope.form);
        console.log(User.nick)
         var x = {name: User.nick, msg: $scope.form.message};
         console.log(x);
        $http({method: 'POST', url: $location.path()+'sendMessage',data:x}).
         success(function(data, status, headers, config) {
        if(status == 200)
        {
          console.log("jee");
        }

      }).
      error(function(data, status, headers, config) {
        console.log("roomctrl error");
        // called asynchronously if an error occurs
        // or server returns response with an error status.
      });
    }

     /*  var handleCallback = function (msg) {
        $scope.$apply(function () {
           // $scope.msg = JSON.parse(msg.data);
           console.log("asdf");
           $scope.channel.messages.push(msg.data);

        });openConnections.push(res);
    };*/
    $http({method: 'GET', url: $location.path()}).
          success(function(data, status, headers, config) {
            $scope.channel = data;
          }).
          error(function(data, status, headers, config) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
          });

   $scope.msg = {};
 
        // handles the callback from the received event
        var handleCallback = function (msg) {
            $scope.$apply(function () {
                $scope.channel.messages.push(msg.data.toString());

            });
        }

        var source = new EventSource($location.path()+'update');
        source.addEventListener('message', handleCallback, false);

   
}]);